<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>call&&apply手写</title>
  </head>
  <body>
    <script>
      Function.prototype.call2 = function (thisArg) {
        var thisArg = thisArg || window;
        var args = [];
        for (var i = 1; i < arguments.length; i++) {
          args.push('arguments[' + i + ']');
        }
        thisArg.fn = this;
        // 通过eval来实现
        var res = eval('thisArg.fn(' + args.join(',') + ')');
        delete thisArg.fn;
        return res;
      };
      function getName(a) {
        console.log(this.name + ':' + a);
      }
      var obj = {
        name: 'Samuel',
      };
      getName.call2(obj, 'rnm');
      var name = 'window';
      getName.call2(null, '退钱');
    </script>
  </body>
</html>
